<?php
if(!isset($_SESSION)) 
{ 
session_start(); 
} 

include '../../connection/connection.php';
include "../../value/value_kr.php";	
$monitor = $_REQUEST['monitor'];
$monitorname=$_REQUEST['monitorname'];
	
	$list=$_SESSION['sort'];
	if(!isset($_REQUEST['page'])){
		$list["cars.szUser"]="ASC";
		$list["cars.szTeam"]="DESC";
		$list["cars.szPhone"]="DESC";
		$list["cars.szMobile"]="DESC";
		$list["cars.szPower"]="DESC";
		$list["cars.szIP"]="DESC";

		$str="cars.szUser";
		
		$_SESSION['sortn']=$str;
		$_SESSION['sort']=$list;
	}else{
		$str=$_SESSION['sortn'];
		$list=$_SESSION['sort'];
	}
	
	$sql = "SELECT cars.* from caruser cars";
	$selectuser = mysql_query($sql." ORDER BY ".$str." ".$list[$str],$connection);
	$_SESSION['sort']=$list;
	

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="x-ua-compatible" content="IE=edge" />
<link href="../../css/style.css" rel="stylesheet" type="text/css" />
<link href="../../popup-window.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../js/jquery.js"></script>
<script type="text/javascript" src="../../popup-window.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	ScrollTable();
	
});
function MyResize(){
	ScrollTable();
}

function ScrollTable(){
	wid = $("table#tablebody2").parent().css('width');
	wid = wid.slice(0,-2)-17;
	$("table#tablebody2").css("width", wid);
	$("table#tablebodyheader").css("width", wid);
	$('table#tablebody2').parent().scrollTop(0);
	OnTRCSS();
}
function OnTRCSS(){
	$("table.tbchung tr:even").each(function(){
		$(this).css("background-color","#eff3fa");
	});
}
</script>
<script type="text/javascript">
$(function() {
	 var checkexistus = '<?=$_SESSION['iduser']?>';
	if(checkexistus==''){
		admin_logout();
		}
});
function resetUser(){
	$("#txtUser").removeAttr('readonly');
	document.getElementById('txtUser').style.backgroundColor ="#ffffff";
	
	document.getElementById('txtUser').value="";
	document.getElementById('txtpass').value="";
	document.getElementById('txtdepart').value="";
	document.getElementById('txthomephone').value="";
	document.getElementById('txtIP').value="";
	document.getElementById('txtconfirmPass').value="";
	document.getElementById('txttelphone').value="";

	document.getElementById('buttonedit').setAttribute("class","inbtnDisable");
	document.getElementById('buttonedit').className = "inbtnDisable";

	document.getElementById('buttondelete').setAttribute("class","inbtnDisable");
	document.getElementById('buttondelete').className = "inbtnDisable";
	
	document.getElementById('buttonedit').disabled = true;
	document.getElementById('buttondelete').disabled = true;
	document.getElementById('buttonadd').setAttribute("class","inbtn");
	document.getElementById('buttonadd').className = "inbtn";
	
	document.getElementById('buttonadd').disabled = false;
}

function onclickRowUser(els,idrowuser){
	$('#tablebody2 tr.select').removeClass('select');
	$(els).addClass('select');
	
	document.getElementById('idrowuser').value = idrowuser;
	document.getElementById('idhiddenuser').value = idrowuser;
	
	$.ajax({url:"detail-user.php?idrowuser="+idrowuser,success:function(result){
		document.getElementById('iddetailuser').innerHTML = result;
		document.getElementById('buttonadd').setAttribute("class","inbtnDisable");
		document.getElementById('buttonadd').className = "inbtnDisable";
		
		document.getElementById('buttonedit').setAttribute("class","inbtn");
		document.getElementById('buttonedit').className = "inbtn";
		
		document.getElementById('buttondelete').setAttribute("class","inbtn");
		document.getElementById('buttondelete').className = "inbtn";
		
		document.getElementById('buttonedit').disabled = false;
		document.getElementById('buttondelete').disabled = false;

		document.getElementById('txtUser').style.backgroundColor = "#CCCCCC";
		$('#txtUser').attr('readonly',true);
		}
	});
}
function editUser(){
	var idrowuser = document.getElementById('idhiddenuser').value;
	var username = $('#txtUser').val();
	var ipuser = $('#txtIP').val();
	var passw = $('#txtpass').val();
	var repass = $('#txtconfirmPass').val();
	var depart = $('#txtdepart').val();
	var homephone = $('#txthomephone').val();
	var telphone = $('#txttelphone').val();
	var roleuser = $('#roleuser').val();

	if(passw.length==0){
		alert('password is not null');
		return false;
	}else if(repass.length==0){
		alert('repass is not null');
		return false;
	}else if(passw!=repass){
		alert('pass and repass not correctly');
		return false;
	}else if(ipuser.length==0){
		alert('ip user is not null');
		return false;	
	}
	var winH = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	$.ajax({url:"edit-user.php?idrowuser="+idrowuser+"&username="+username+"&depart="+depart+"&homephone="+homephone+"&telphone="+telphone+"&passw="+passw+"&roleuser="+roleuser+"&ipuser="+ipuser+"&winH="+winH,success:function(result){
		document.getElementById('tablecontentUser').innerHTML = result;
		ScrollTable();
		alert("<?=$l_alerteditsuccessful?>");
		resetUser();
		}
	});
}
function deleteUser(){
	var idrowuser = document.getElementById('idhiddenuser').value;
	var winH = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	$.ajax({url:"deleteUser.php?idrowuser="+idrowuser+"&winH="+winH,success:function(result){
		document.getElementById('tablecontentUser').innerHTML = result;
		resetUser();
		ScrollTable();
		alert('<?=$l_alertdeletesuccessful?>');
		}
	});
}
function checkUserExist(){
		var username = $('#txtUser').val();
		var ipuser = $('#txtIP').val();
		var passw = $('#txtpass').val();
		var repass = $('#txtconfirmPass').val();
		var depart = $('#txtdepart').val();
		var homephone = $('#txthomephone').val();
		var telphone = $('#txttelphone').val();
		var roleuser = $('#roleuser').val();

		if(username.length==0){
			alert('username is not null');
			return false;
		}else if(passw.length==0){
			alert('password is not null');
			return false;
		}else if(repass.length==0){
			alert('repass is not null');
			return false;
		}else if(passw!=repass){
			alert('pass and repass not correctly');
			return false;
		}else if(ipuser.length==0){
			alert('ip user is not null');
			return false;	
		}
		
	   $.ajax({url: "validation_user.php?username="+username,success: function(result){
		   		//alert(result);
		   		document.getElementById('dvusernamehidden').innerHTML = result;
				var results = document.getElementById('usernamehidden').value;
		   		if(results=='no'){
			   		alert('user already existed');
		   		}else{
		   			var winH = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
					$.ajax({url:"insert-user-admin.php?username="+username+"&depart="+depart+"&homephone="+homephone+"&telphone="+telphone+"&passw="+passw+"&roleuser="+roleuser+"&ipuser="+ipuser+"&winH="+winH,success: function(result){
							document.getElementById('tablecontentUser').innerHTML = result;
							alert("<?=$l_alertinsertsuccessful?>");
							ScrollTable();
							resetUser();
							}
					});
			   	}
	            //document.getElementById('tablecontentUser').innerHTML = result;
	          }
	});
}
function sortAndPagingUser(string,flagDesc){
	var winH = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	$.ajax({url:"sortAndPagingUser.php?string="+string+"&flagDesc="+flagDesc+"&winH="+winH,success:function(result){
		var expr = new RegExp('>[ \t\r\n\v\f]*<', 'g');
		var response_html_fixed = result.replace(expr, '><');
		document.getElementById('tablecontentUser').innerHTML = response_html_fixed;
		ScrollTable();
		}
	});
}

function admin_logout(){
	window.location="../../view_logout.php?changepage=admin";
}
function onclickSearch(){
	window.location = "../../search/searchcarnums.php?monitor=<?=$monitor?>&monitorname=<?=$monitorname?>";
}
function onclickView(){
	window.location = "../../index.php?viewaction=none";
}

function loadpos(){
	var winHf = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	$("#divscroll").css("height", parseInt(winHf - 240));
}

function ajaxLoadContentUser(){
	var winH = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
	$.ajax({url:"ajax_load_content_user.php?winH="+winH,success:function(result){
		document.getElementById('tablecontentUser').innerHTML = result;
		ScrollTable();
		}
	});
}
</script>
</head>
<?php include 'show_function_popup_carcrime.php';?>
<body onresize="MyResize();" onload="loadpos();ajaxLoadContentUser();load_popup_carCrime();">
<p id="maylya" ><input type="hidden"  id="mayly" name="mayly" /></p>
<div id="dvusernamehidden" style="display: none;"><input type="hidden" id="usernamehidden"></div>
   <div id="menu" style="text-align: center;">
    	<ul class="amenu">
        	<li><a onclick="onclickSearch();"><input class="csssearch" type="button"/></a></li>
            <li style="background:#1e7bbd"><a href=""><input class="cssadminchoose" type="button"/></a></li>
            <li style="width: 215px;"><a onclick="onclickView();"><input class="cssview" type="button"/></a>
            	<select class="seltopadmin"  disabled="disabled">
            		<option><?php echo $monitorname;?></option>
            	</select>
            </li>
            <li><a onclick="admin_logout()" style="cursor:pointer" ><input class="csslogout" type="button"/></a></li>
        </ul>
         <div class="clear"></div>
    </div>
   <div id="submenu">
    	<ul class="asubmenu">
        	<li><a href="../content-monitor-admin.php?monitor=<?=$monitor?>&monitorname=<?=$monitorname?>"><?php echo $l_monitor?></a></li>
            <li><a href="../camera/content-camera-admin.php?monitor=<?=$monitor?>&monitorname=<?=$monitorname?>"><?php echo $l_camera?></a></li>
           <li><a href="../car-crime-input-by-user/car-crime-input-by-user-admin.php?monitor=<?=$monitor?>&monitorname=<?=$monitorname?>"><?php echo $l_carcrimeuser?></a></li>
            <li><a href="../car-crime/content-car-crime-admin.php?monitor=<?=$monitor?>&monitorname=<?=$monitorname?>"><?php echo $l_carcrime?></a></li>
            <li><a href="../PC/content-PC-admin.php?monitor=<?=$monitor?>&monitorname=<?=$monitorname?>">PC</a></li>
            <li><a href="#" style="background:#034776; border-radius:6px"><?php echo $l_user?></a></li>
        </ul>
        <div class="clear"></div>
    </div>
   <div class="main">
   	<div id="iddetailuser">
        <table cellpadding="0" cellspacing="0" border="0" width="90%" class="tbm1" style="margin-bottom:10px">        	
            <tr>
                <td><?php echo $l_usern?></td>
                <td><input type="text" class="intext" style="width:150px;padding-left: 5px;line-height: 26px;text-align: left;" id="txtUser" name="txtUser"/></td>
                <td><?php echo $l_passw?></td>
                <td><input type="password" class="intext" style="width:150px;padding-left: 5px;line-height: 26px;text-align: left;" id="txtpass" name="txtpass"/></td>
                <td><?php echo $l_dept?></td>
                <td><input type="text" class="intext" style="width:150px;padding-left: 5px;line-height: 26px;text-align: left;" id="txtdepart" name="txtdepart"/></td>
                <td><?php echo $l_homephone?></td>
                <td><input type="text" class="intext" style="width:150px;padding-left: 5px;line-height: 26px;text-align: left;" id="txthomephone" name="txthomephone"/></td>
                <td><?php echo $l_role?></td>
                <td>
                	<select class="seltop" size="1" name="roleuser" id="roleuser">
                		<option value="Admin" id="Admin">Admin</option>
                		<option value="User" id="User">User</option>
					</select>
				</td>
                <td><input type="button" class="inbtn" style="width: 65px;"  value="<?php echo $l_new?>" id="buttonadd" name="buttonadd" onclick="checkUserExist();"/></td>
                <td><input type="button" class="inbtn" style="width: 65px;"  value="<?php echo $l_reset?>" onclick="resetUser();" id="buttonreset" name="buttonreset"/></td>
            </tr>   
           <tr>
                <td>IP</td>
                <td><input type="text" class="intext" style="width:150px;padding-left: 5px;line-height: 26px;text-align: left;" id="txtIP" name="txtIP"/></td>
                <td><?php echo $l_confirmpass?></td>
                <td><input type="password" class="intext" style="width:150px;padding-left: 5px;line-height: 26px;text-align: left;" id="txtconfirmPass" name="txtconfirmPass"/></td>
                <td></td>
                <td></td>
                <td><?php echo $l_mobilephone?></td>
                <td><input type="text" class="intext" style="width:150px;padding-left: 5px;line-height: 26px;text-align: left;" id="txttelphone" name="txttelphone"/></td>
                <td></td>
                <td></td>
                <td><input type="button" class="inbtnDisable" style="width: 65px;"  value="<?php echo $l_edit?>"  id="buttonedit" name="buttonedit"  disabled="disabled"
                	 onclick="editUser();"/></td>
                <td><input type="button" class="inbtnDisable" style="width: 65px;"  value="<?php echo $l_delete?>" id="buttondelete" name="buttondelete" disabled="disabled"
                	onclick="if(confirm('<?=$l_areyousure?>')){ deleteUser();return true;}else{return false;}"/></td>
            </tr>
        </table>
        </div>
          <div class="divtheader">
           	<table cellpadding="0" cellspacing="0" border="0" width="100%" class="tbchung" id="tablebodyheader">
           		<thead>
		            <tr>
		            	<th style="width: 5%">No</th>
		                <th style="width: 25%"><a onclick="sortAndPagingUser('cars.szUser',1)" style="cursor: pointer;color: #027acc"><?php echo $l_user?></a></th>
		                <th style="width: 15%"><a onclick="sortAndPagingUser('cars.szTeam',1)" style="cursor: pointer;color: #027acc"><?php echo $l_dept?></a></th>
		                <th style="width: 13.75%"><a onclick="sortAndPagingUser('cars.szPhone',1)" style="cursor: pointer;color: #027acc"><?php echo $l_homephone?></a></th>
		                <th style="width: 13.75%"><a onclick="sortAndPagingUser('cars.szMobile',1)" style="cursor: pointer;color: #027acc"><?php echo $l_mobilephone?></a></th>
		                <th style="width: 13.75%"><a onclick="sortAndPagingUser('cars.szPower',1)" style="cursor: pointer;color: #027acc"><?php echo $l_role?></a></th>
		                <th class="last" style="width: 13.75%;border-right: none !important;"><a onclick="sortAndPagingUser('cars.szIP',1)" style="cursor: pointer;color: #027acc">IP</a></th>
		            </tr>
	            </thead>
           	</table>
           </div>
        <div id="tablecontentUser" style="width: 100%;">
         	<div id="divscroll" style="overflow:scroll;overflow-x: hidden; width:100%; height:480px; background-color: #ffffff">
		        <table cellpadding="0" cellspacing="0" border="0" width="100%" class="tbchung" id="tablebody2">
		            <tbody>
		           
		            </tbody>              
		        </table>
	        </div>
	    </div>
        <input type="hidden" id="idhiddenuser" name="idhiddenuser" value="">
            <?php include 'show_content_popup_carcrime.php';?>
    </div>
</body>
 <?php mysql_close($connection);?>